import sys, json, pickle

if sys.version_info[0] >= 3:
    raw_input = input

from ddbms.ExecuterSite import ExecuterSite
from ddbms.StaticSchema import *
from ddbms.TrackerSite import TrackerSite
from ddbms.Json import dump,read
import MySQLdb as mdb, time
if __name__ == '__main__':    
    schema = StaticSchema()    
    str = dump(schema)
    print(read(str))
    mysql = ("localhost","root","","ddb")
    es = ExecuterSite(schema,mysql)
    time.clock()
    ra = es.parse("""    select publisher.name  from Publisher
    """)
    ra = es.expand(ra)
    ra = es.optimize(ra)
    ex = es.allocate(ra)    
    result = es.execute(ex)
        
    str = pickle.dumps(result.eval())
    print(pickle.loads(str))  
    t = time.clock()    
    print("time: %d"%t)
    
    
#    s = SqlStatement()
#    st = s.parse("select * from book where book.id<book.id2 and ((book.id>1 and book.id<=5) or book.id=10) and book.name='newbook'")
#    cond = st.condition.toCNF()
#    for i in range(0,15,1):
#        for j in range(0,15,1):
#            nmap = {"book.id":i,"book.name":"newbook","book.id2":j}
#            if cond.test(nmap):
#                print(nmap)
#    conn = mdb.connect('localhost', 'root', '', 'ddb')
#    cursor = conn.cursor(mdb.cursors.DictCursor)
#    cursor.execute("SELECT * from customer")
#    print (len(cursor.fetchall()))